package com.weactivity.mapper;

import com.weactivity.entity.Activity;
import org.apache.ibatis.annotations.*;

import com.github.pagehelper.Page;
import com.weactivity.dto.PageQueryDTO;
import com.weactivity.dto.ResourceDTO;
import com.weactivity.entity.Resource;
import org.apache.ibatis.annotations.Mapper;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface ResourceMapper {
    @Insert("insert into resources ( aid, stage, name, type, movable, position, imp, overhead, appoint, appoint_time, begin_time, end_time) value"+
            "( #{aid}, #{stage}, #{name}, #{type}, #{movable}, #{position}, #{imp}, #{overhead}, #{appoint}, #{appoint_time}, #{beginTime}, #{endTime})")
    int insert(Resource resource);

    Page<ResourceDTO> pageQuery(PageQueryDTO pageQueryDTO);

    void update(Resource resource);

    void updateById(Integer id);

    List<Resource>  selectResourceByName(@Param("resourceName") String resourceName, @Param("actBeginDate") LocalDate actBeginDate);

    //根据日期查找活动当天需要的资源的占用情况,包括没有被占用的资源
    //如果当天某个资源有占用情况，返回占用情况但不返回该资源的空闲状态
    //如果当天某个资源没被占用，返回该资源的空闲状态
    List<Resource> selectResourceByTypeAndImp(@Param("resourceType") int resourceType, @Param("resourceImp") int resourceImp, @Param("actBeginDate") LocalDate actBeginDate);

    //查找资源的名称名称
    @Select("SELECT DISTINCT name " +
            "FROM resources " +
            "WHERE type = #{resourceTypeInt} " +
            "  AND imp = #{resourceImpInt}")
    List<String> selectResourceName(@Param("resourceTypeInt") int resourceTypeInt,
                                    @Param("resourceImpInt") int resourceImpInt);

    int updateUseAct(@Param("id") int id,@Param("aid") int aid,@Param("appointTime") String appointTime, @Param("beginTime") String beginTime,@Param("endTime") String endTime);



    @Select("select distinct type , imp  from resources where name = #{resourceName};")
    Map<String, Object> selectTypeAndImp(@Param("resourceName") String resourceName);


    List<Resource> findSimilarResource(Activity activity, @Param("type") int type, @Param("imp") int imp);


    @Delete("delete from resources where id = #{id} ;")
    int deleteResourceById(int id);

    @Insert("insert into resources (aid,name,type,movable,imp,appoint,appoint_time,begin_time,end_time) values" +
            " (#{act.id},#{name},#{type},0,#{imp},1,now(),#{act.beginTime},#{act.endTime});")
    int insertResourceRecord(@Param("act") Activity activity, @Param("name") String name,@Param("type") int type,@Param("imp") int imp);

    @Delete("delete from resources where aid = #{id} ;")
    int deleteResourceByAid(int id);

    @Select("select * from resources where aid = #{act.id} and name = #{name} and type = #{type} and imp = #{imp} ;")
    Resource findResource(@Param("act") Activity activity,@Param("name") String name,@Param("type") int type,@Param("imp") int imp);

//    @Insert()
//    Resource insertResource(Resource resource);
}
